/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.game.entitysystem.systems;

import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.scene.Node;
import etherblocks.engine.objects.ModelObject;
import etherblocks.engine.states.*;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;

/**
 *
 * @author Carl
 */
public class PhysicsSystem extends ModelObjectSystem{

    public PhysicsSystem(Node rootNode, AppStateManager appStateManager){
        super(rootNode);
        this.appStateManager = appStateManager;
    }
    private AppStateManager appStateManager;
    
    @Override
    public void onNextFrameCalculation(EntityMap entityMap, float lastTimePerFrame){
        for(Entity entity : entityMap.getEntities(PhysicsComponent.class)){
            ModelObject modelObject = getModelObject(entity);
            if(modelObject != null){
                BulletAppState bulletAppState = appStateManager.getState(BulletAppState.class);
                CharacterControl characterControl = modelObject.getControl(CharacterControl.class);
                if(characterControl == null){
                    characterControl = createCharacterControl(entity);
                    modelObject.addControl(characterControl);
                    bulletAppState.getPhysicsSpace().add(characterControl);
                    characterControl.setPhysicsLocation(entity.getComponent(LocationComponent.class).getLocation());
                    characterControl.setViewDirection(entity.getComponent(RotationComponent.class).getViewDirection());
                    if(entity.getID() == getPlayerEntityID()){
                        appStateManager.getState(PlayerAppState.class).onPlayerModelChanged();
                    }
                }
                else if(entity.getComponent(RemovedComponent.class) != null){
                    bulletAppState.getPhysicsSpace().remove(characterControl);
                }
            }
        }
    }
    
    private CharacterControl createCharacterControl(Entity entity){
        CollisionShape collisionShape = null;
        CapsuleCollisionShapeComponent capsuleCollisionComponent = entity.getComponent(CapsuleCollisionShapeComponent.class);
        BoxCollisionShapeComponent boxCollisionComponent = entity.getComponent(BoxCollisionShapeComponent.class);
        if(capsuleCollisionComponent != null){
            collisionShape = new CapsuleCollisionShape(capsuleCollisionComponent.getRadius(), capsuleCollisionComponent.getHeight());
        }
        else if(boxCollisionComponent != null){
            collisionShape = new BoxCollisionShape(boxCollisionComponent.getSize().divide(2));
        }
        return new CharacterControl(collisionShape, 0.05f);
    }
    
    private int getPlayerEntityID(){
        return appStateManager.getState(NetworkAppState.class).getNetworkClient().getGameClient().getPlayerEntityID();
    }
}
